import 'package:flutter_book/api/api_common.dart';
import 'package:flutter_book/data/model/yck/rule/parse_factory.dart';
import 'package:flutter_book/data/model/yck/yck_rule_book_3_model.dart';
import 'package:flutter_book/util/analyze/search_yck_rule_book_util.dart';
import 'package:html/parser.dart';
import 'package:html/dom.dart';
import 'package:flutter_book/util/extensions/string_extensions.dart';

class AnalyzeYckRuleBookUtil {
  static String? htmlString;

  static AnalyzeYckRuleBookUtil? _util;

  static AnalyzeYckRuleBookUtil init() {
    if (_util == null) {
      _util = AnalyzeYckRuleBookUtil();
    }
    return _util!;
  }

  searchBookList(YckRuleBook3Model model, String key, [int page = 0]) async {
    // if(htmlString == null) {
    //   htmlString = await SearchYckRuleBookUtil.fromRule(model, key, page).searchBooks();
    // }
    String? htmlString = await SearchYckRuleBookUtil.fromRule(model, key, page).searchBooks();
    try {
      if (htmlString != null) {
        analyzeHtml(htmlString, model);
      } else {
        "资源获取失败1${model.bookSourceName},${model.bookSourceUrl}".print;
      }
    } catch (e) {
      "资源获取失败2${model.bookSourceName},${model.bookSourceUrl}".print;
    }
  }

  analyzeHtml(String htmlString, YckRuleBook3Model model) async {
    // var document = parse(htmlString);

    // htmlString.print;

    // "booklist:${model.ruleSearch?.bookList}".print;
    // "name:${model.ruleSearch?.name}".print;
    // "author:${model.ruleSearch?.author}".print;
    // "intro:${model.ruleSearch?.intro}".print;
    // "kind:${model.ruleSearch?.kind}".print;
    // "lastChapter:${model.ruleSearch?.lastChapter}".print;
    // "updateTime:${model.ruleSearch?.updateTime}".print;
    // "bookUrl:${model.ruleSearch?.bookUrl}".print;
    // "coverUrl:${model.ruleSearch?.coverUrl}".print;
    // "wordCount:${model.ruleSearch?.wordCount}".print;

    // try {
    var book = await ParseFactory.getParser(model.ruleSearch!.bookList).getStringList(htmlString);

    "资源获取成功了:${book.length}个数,${model.bookSourceName},${model.bookSourceUrl}".print;
    // if(book.length>0){
    //   book[0].print;
    // }

    // } catch (e) {
    //   "资源获取失败了:${model.bookSourceName},${model.bookSourceUrl}个数".print;
    // }
  }

  parseBook(YckRuleBook3Model source, String content) async {
    var book = {};
    book['name'] = await ParseFactory.getParser(source.ruleSearch!.name).getString(content);
    book['author'] = await ParseFactory.getParser(source.ruleSearch!.author).getString(content);
    book['coverUrl'] = await ParseFactory.getParser(source.ruleSearch!.coverUrl).getString(content);
    book['intro'] = await ParseFactory.getParser(source.ruleSearch!.intro).getString(content);
    book['kind'] = await ParseFactory.getParser(source.ruleSearch!.kind).getString(content);
    book['lastChapter'] = await ParseFactory.getParser(source.ruleSearch!.lastChapter).getString(content);
    book['updateTime'] = await ParseFactory.getParser(source.ruleSearch!.updateTime).getString(content);
    book['wordCount'] = await ParseFactory.getParser(source.ruleSearch!.wordCount).getString(content);
    book['bookUrl'] = await ParseFactory.getParser(source.ruleSearch!.bookUrl).getString(content);
    book['bookList'] = await ParseFactory.getParser(source.ruleSearch!.bookList).getString(content);
    return book;
  }


  String? getSearchUrl(YckRuleBook3Model model) {

  }
}
